/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-08-28     chenbin       the first version
 */
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <u8g2_port.h>
#include "string.h"


extern void u8g2_ssd1306_12864_4wire_sw_spi_example(int argc,char *argv[]);

extern u8g2_t * u8g2_context_get(void);

void encoder_init(void);
int encoder_pos_get(void);


extern const uint8_t font_12_map[];
extern const uint8_t font_10_map[];

uint8_t lcd_buffer[128] = {0};

#include "language_entry.h"

void app_lcd_menu_init(void)
{
    u8g2_t * p_u8g2 = NULL;

    encoder_init();

    u8g2_ssd1306_12864_4wire_sw_spi_example(0,NULL);

    p_u8g2 = u8g2_context_get();

    rt_kprintf("menu init\n");

    // Draw Graphics
    /* full buffer example, setup procedure ends in _f */
    u8g2_ClearBuffer(p_u8g2);
    //u8g2_SetFont(p_u8g2, u8g2_font_ncenB08_tr);
    u8g2_SetFont(p_u8g2, font_12_map);
    u8g2_DrawStr(p_u8g2, 1, 18, "U8g2 on RT-Thread");
    u8g2_SendBuffer(p_u8g2);

    rt_thread_mdelay(1000);

    int32_t val_new = 0;
    int32_t val_old = 0;
    int32_t val_index = 0;
    while(1)
    {
        val_new = encoder_pos_get();
        if(val_old != val_new)
        {
            val_old = val_new;

            u8g2_ClearBuffer(p_u8g2);
            //u8g2_DrawStr(p_u8g2, 1, 16 - 4, "U8g2 on RT-Thread");

            rt_snprintf(lcd_buffer,128, "encoder: %d",val_old / 4);
            //u8g2_DrawStr(p_u8g2, 1, 16 - 4, lcd_buffer);
            u8g2_DrawUTF8(p_u8g2, 1, 16 - 4, lcd_buffer);

            val_index = val_old / 4;
            val_index %= language_entry_size();

            //显示中文
            //u8g2_SetFont(p_u8g2, u8g2_font_wqy12_t_chinese3);
            //u8g2_SetFont(p_u8g2, font_10_map);

            language_index_set(0);
            rt_snprintf(lcd_buffer,128, "%s",language_entry_get(val_index));
            //u8g2_DrawStr(p_u8g2, 1, 32 - 4, lcd_buffer);
            u8g2_DrawUTF8(p_u8g2, 1, 32 - 4, lcd_buffer);


            //显示拉丁语
            //u8g2_SetFont(p_u8g2, font_10_map);

            language_index_set(1);
            rt_snprintf(lcd_buffer,128, "%s",language_entry_get(val_index));
            //u8g2_DrawStr(p_u8g2, 1, 48 - 4, lcd_buffer);
            u8g2_DrawUTF8(p_u8g2, 1, 48 - 4, lcd_buffer);


            language_index_set(2);
            rt_snprintf(lcd_buffer,128, "%s",language_entry_get(val_index));
            //u8g2_DrawStr(p_u8g2, 1, 64 - 4, lcd_buffer);
            u8g2_DrawUTF8(p_u8g2, 1, 64 - 4, lcd_buffer);

            u8g2_SendBuffer(p_u8g2);
        }
        rt_thread_mdelay(10);
    }
}

